package service;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

/**
 * @author yk
 * @version 1.0
 * @date 2021/2/26
 */
public class RequestProcessor extends Thread {

    private Socket accept;
    private Map<String,HttpServlet> map;



    public RequestProcessor(Socket accept, Map<String, HttpServlet> map) {
        this.accept = accept;
        this.map = map;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = accept.getInputStream();
            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(accept.getOutputStream());
            if ( map.get( request.getUrl() ) ==null ){
                response.outputHtml(request.getUrl());
            }else {
                map.get(request.getUrl()).service(request,response);
            }
            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
